package jrobutt.room.menu.titlescreen;

import jrobutt.*;
import jrobutt.room.menu.MenuItem;
import jrobutt.room.menu.levelselection.LevelSelectionRoom;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.*;
import java.util.List;

public class TitleScreenRoom extends GameRoom
{
    ArrayList<MenuItem> menu_items;
    
    protected Collection<? extends KeyboardResponder> getKeyboardResponders ()
    {
        return menu_items;
    }
    protected Collection<? extends Renderable> getRenderables ()
    {
        return menu_items;
    }
    protected Collection<? extends Updatable> getUpdatables () { return null; }
    
    public TitleScreenRoom ()
    {
        menu_items = new ArrayList<MenuItem>(4);
    }
    
    public Color get_bg_color ()
    {
        return Settings.bgcolor;
    }
    
    public void enter ()
    {
        String[] names = new String[]{
            "start",
            "do_nothing",
            "do_absolutely_nothing"
        };
        MenuItem[] temp_menu_items = new MenuItem[names.length];
        
        float root_x = Settings.width / 2.0f;
        float root_y = Settings.height / 2.0f;
        float x = root_x;
        float y = root_y;
        for (int k=0; k<names.length; k++)
        {
            String menu_text = names[k];
            
            GameSprite sprite_selected = new GameSprite(Global.image_loader.load("menu/" + menu_text + "-selected.png"), 0.5f, 0.5f);
            GameSprite sprite = new GameSprite(Global.image_loader.load("menu/" + menu_text + ".png"), 0.5f, 0.5f);
            temp_menu_items[k] = new MenuItem(x, y, false, sprite_selected, sprite, null, null, null, null);
            y += sprite.height;
        }
        
        for (int k=0; k<temp_menu_items.length; k++)
        {
            MenuItem menu_item = temp_menu_items[k];
            menu_item.down = temp_menu_items[(k+1)%temp_menu_items.length];
            menu_item.up = temp_menu_items[(k+temp_menu_items.length-1)%temp_menu_items.length];
            menu_items.add(menu_item);
        }
        temp_menu_items[0].is_selected = true;
        
        // hax hax hax this is terrible and hardcoded and terrible
        menu_items.add(new MenuItem(root_x, 128.0f, false, null, new GameSprite(Global.image_loader.load("menu/title.png"), 0.5f, 0.5f), null, null, null, null));
    }
    public void exit ()
    {
        menu_items.clear();
    }
    
    public GameRoom key_pressed (int key)
    {
        if (key == KeyEvent.VK_ESCAPE)
        {
            return Global.gameover_room;
        }
        
        if (key == KeyEvent.VK_ENTER)
        {
            if (menu_items.get(0).is_selected)
            {
                return new LevelSelectionRoom();
            }
        }
        
        for (MenuItem menu_item : menu_items)
        {
            if (!menu_item.is_selected) continue;
            
            menu_item.key_pressed(key);
            break;
        }
        return null;
    }
    
    public GameRoom update ()
    {
        return null;
    }
}
